#!/bin/bash

# while-menu-dialog: a menu driven system information program

DIALOG_CANCEL=1
DIALOG_ESC=255
HEIGHT=0
WIDTH=0

display_result() {
  dialog --title "$1" \
    --no-collapse \
    --msgbox "$result" 0 0
}

while true; do
  exec 3>&1
  selection=$(dialog \
    --backtitle "Dotfiles master v1.1" \
    --title "Menu" \
    --clear \
    --cancel-label "Exit" \
    --menu "Please select:" $HEIGHT $WIDTH 4 \
    "1" "Inspect" \
    "2" "Backup Host" \
    "3" "Backup Global" \
    "4" "Install Host" \
    "5" "Install Global" \
    2>&1 1>&3)
  exit_status=$?
  exec 3>&-
  case $exit_status in
    $DIALOG_CANCEL)
      clear
      echo "Program terminated."
      exit
      ;;
    $DIALOG_ESC)
      clear
      echo "Program aborted." >&2
      exit 1
      ;;
  esac
  case $selection in
    1 )
      vim dotfiles
      ;;
    2 )
      mkdir $HOSTNAME
      cp ~/.bashrc ./$HOSTNAME
      cp ~/.conkyr* ./$HOSTNAME
      cp ~/.gtkrc-2.0 ./$HOSTNAME
      cp ~/.xinitrc ./$HOSTNAME
      cp ~/.Xresources ./$HOSTNAME
      cp -r ~/.config/ ./$HOSTNAME
      ;;
    3 )
      cp ~/.bashrc ./
      cp ~/.conkyr* ./
      cp ~/.gtkrc-2.0 ./
      cp ~/.xinitrc ./
      cp ~/.Xresources ./
      cp -r ~/.config/ ./
      ;;
    4 )
      cp ./$HOSTNAME/.bashrc ~/
      cp ./$HOSTNAME/.conkyr* ~/
      cp ./$HOSTNAME/.gtkrc-2.0 ~/
      cp ./$HOSTNAME/.xinitrc ~/
      cp ./$HOSTNAME/.Xresources ~/
      cp -r ./$HOSTNAME/.config/ ~/
      ;;
    5 )
      cp .bashrc ~/
      cp .conkyr* ~/
      cp .gtkrc-2.0 ~/
      cp .xinitrc ~/
      cp .Xresources ~/
      cp -r .config/ ~/
      ;;
  esac
done
